package com.gmail.ceac13;

import java.util.List;

import com.gmail.ceac13.servicos.ImageServer;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PrincipalAdapter extends BaseAdapter{
	
	/**
	 * Guarda o contexto.
	 */
	private Context context;
	
	/**
	 * Lista de amigos do usuario.
	 */
	private List<Friend> lista;
	private FriendDao dao;
	
	public PrincipalAdapter(Context context, List<Friend> lista) {
		this.context = context;
		this.lista = lista;
	}
	
	public PrincipalAdapter(Context context) {
		this.context = context;
		dao = new FriendDao(this.context);
		this.atualizarAdapter();
	}

	public void atualizarAdapter() {
		this.lista = dao.findAllFriends();
	}
	
	@Override
	public int getCount() {
		return this.lista.size();
	}

	@Override
	public Object getItem(int arg0) {
		return this.lista.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int posicao, View view, ViewGroup parent) {
		Friend friend = lista.get(posicao);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.item, null);
		
		TextView nome = (TextView)v.findViewById(R.id.nome);
		TextView distancia = (TextView)v.findViewById(R.id.distancia);
		ImageView foto = (ImageView) v.findViewById(R.id.foto);
		
		nome.setText(friend.getNome());
		int d = friend.getDistancia();
		String dist;
		if (d >= 1000)
			dist = (d/1000) + "km";
		else
			dist = d + "m";
		distancia.setText(dist);

		Log.i("ConnectME", "getFoto() = "+friend.getFoto());
		if (friend.getFoto().equals("-1"))
			foto.setImageResource(R.drawable.usuariopadrao);
		else
			foto.setImageDrawable(ImageServer.buscarImagem(friend.getFoto(), context));
		return v;
	}

}
